package com.saoft.fangcms.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.saoft.fangcms.common.enums.StatusEnum;
import com.saoft.fangcms.modules.sys.entity.Menu;
import com.saoft.fangcms.modules.sys.entity.Role;
import com.saoft.fangcms.modules.sys.entity.User;
import com.saoft.fangcms.modules.sys.mapper.MenuMapper;
import com.saoft.fangcms.modules.sys.mapper.RoleMapper;
import com.saoft.fangcms.modules.sys.mapper.UserMapper;
import com.saoft.fangcms.modules.sys.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author scc95599
 * @since 2019-12-24
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    MenuMapper menuMapper;

    @Override
    public User getByName(String username) {
        User user = userMapper.selectOne(
                new QueryWrapper<User>().eq("username", username));
        //获取角色
        if (user != null) {
            List<Role> rolesByUserId = roleMapper.getRolesByUserId(user.getId());
            user.setRoles(new HashSet<>(rolesByUserId));

            //设置权限
            if (rolesByUserId != null) {
                for (Role role : rolesByUserId) {
                    List<Menu> menusByRoleId = menuMapper.getMenusByRoleId(role.getId());
                    role.setMenus(new HashSet<>(menusByRoleId));
                }
            }

        }
        return user;
    }

    @Override
    public boolean updateStatus(StatusEnum statusEnum, List<Long> ids) {
        return false;
    }
}
